<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\CityBusiness;
use Cache;
use Exception;
use Illuminate\Http\Request;

/**
 * 城市相关
 */
class CityController extends Controller
{
    /**
     * 获取所有城市
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function get_all_city(Request $request)
    {
        $URI   = $request->getpathInfo();
        $input = $request->all();
        ksort($input);
        $params    = http_build_query($input);
        $cache_key = 'SJ.' . md5($URI) . md5($params);

        if (!Cache::has($cache_key)) {
            $data = [];
            $city = City::where(['is_open' => 2])->whereIn('type', [3, 4])->get();
            //处理数据
            foreach ($city as $key => $value) {
                $data[$key]['cityID']       = $value->cityID;
                $data[$key]['type']         = $value->type;
                $data[$key]['name']         = $value->name;
                $data[$key]['PID']          = $value->PID;
                $data[$key]['lng']          = $value->lng;
                $data[$key]['lat']          = $value->lat;
                $data[$key]['photo']        = $value->city_site->photo;
                $data[$key]['pinyin']       = $value->city_site->pinyin;
                $data[$key]['first_letter'] = $value->city_site->first_letter;
            }

            $return = response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);

            Cache::add($cache_key, $return, 10);

            return $return;
        }
        return Cache::get($cache_key);
    }

    /**
     * 根据城市名称返回城市id
     *
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function get_city_id(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $name   = $request->get('name');
            if (empty($name)) {
                throw new Exception('缺少参数');
            }
            //根据名称查询城市id
            $city = City::where('name', 'like', '%' . $name . '%')->first();
            if (!$city) {
                throw new Exception("未找到城市信息");
            }
            $data['city_id'] = $city->cityID;
            $data['name']    = $city->name;
            $status          = 1;
            $msg             = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 根据城市id获取区
     *
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function get_areas(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $id     = $request->get('city_id');
            if (empty($id)) {
                throw new Exception('缺少参数');
            }
            //根据名称查询城市id
            $city = City::where(['cityID' => $id])->first();
            if (!$city) {
                throw new Exception("未找到城市信息");
            }
            //根据id查找区
            $areas = City::where(['PID' => $id, 'type' => 4])->get();
            //处理数据
            foreach ($areas as $key => $value) {
                $data[$key]['cityID'] = $value->cityID;
                $data[$key]['name']   = $value->name;
                //查找商圈
                $business = CityBusiness::where(['cityID' => $value->cityID])->orderBy('orderby')->get();
                //处理数据
                foreach ($business as $k => $val) {
                    $data[$key]['list'][$k]['id']   = $val->id;
                    $data[$key]['list'][$k]['name'] = $val->name;
                }
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
